package cn.school.controller.workmgr;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.controller.test.FileUploadUtils;
import cn.school.controller.test.Result;
import cn.school.workmgr.entity.RecruitMajor;
import cn.school.workmgr.service.RecruitMajorService;
import cn.school.workmgr.vo.RecruitMajorVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@Api(tags = "招生专业管理")
@RequestMapping("/workmgr/major")
public class RecruitMajorController {

    @Resource
    private RecruitMajorService recruitMajorService;
    @Resource
    private FileUploadUtils fileUploadUtils;
    @Value("${major.picturePathDir}")
    private String filePath;

    @ApiOperation("分页查询")
    @PostMapping("/pageList")
    public CommonResult<CommonPage<RecruitMajor>> list(@RequestBody RecruitMajorVO vo) {
        IPage<RecruitMajor> recruitMajorIPage = recruitMajorService.pageList(vo);
        return CommonResult.success(CommonPage.restPage(recruitMajorIPage));
    }

    @ApiOperation("新增或修改")
    @PostMapping("/addOrUpdate")
    public CommonResult<CommonPage<RecruitMajor>> addOrUpdate(@RequestBody RecruitMajor entity) {
        int num  = recruitMajorService.addOrUpdateMajor(entity);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除单个")
    @PostMapping("/deleteById")
    public CommonResult<CommonPage<RecruitMajor>> deleteById(@RequestBody RecruitMajor entity) {
        int num  = recruitMajorService.deleteById(entity.getId()+"");
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("查询专业")
    @GetMapping("/list/{tenantId}")
    public CommonResult<List<RecruitMajor>> majorList(@PathVariable("tenantId")String tenantId) {
        List<RecruitMajor> list  = recruitMajorService.majorList(tenantId);
        return CommonResult.success(list);
    }

    @ApiOperation("查询专业详情")
    @GetMapping("/listDetails/{id}")
    public CommonResult<RecruitMajor> majorDetails(@PathVariable("id")String id) {
        RecruitMajor recruitMajor  = recruitMajorService.majorDetails(id);
        return CommonResult.success(recruitMajor);
    }
    
    @ApiOperation("上传文件")
    @PostMapping("/uploadImage")
    public Result<String> uploadImage(MultipartFile file) throws IOException {
        return fileUploadUtils.upload(file, filePath);
    }

}
